<template>
	<view class="AppH5login">
		<view class="h5login">
			<view class="login_image">
			</view>
			<view class="login_info">
				<view class="login_phone">
					用&nbsp;户&nbsp;名:<input class="IdInput ipInput" placeholder="请输入昵称" type="text" v-model="username" />
				</view>
				<view class="login_phone">
					手&nbsp;机&nbsp;号:<input class="IdInput" placeholder="请输入手机号" type="text" v-model="phone" />
				</view>
				<view class="login_phone">
					登录密码:<input placeholder="请输入登录密码" type="text" password v-model="phonePassword" />
				</view>
				<view class="login_phone">
					确认密码:<input placeholder="请确认密码" type="text" password v-model="newPassword" />
				</view>
				<view class="login_phone">
					验&nbsp;证&nbsp;码:<input class="IdInput" placeholder="请输入验证码" type="text" v-model="password" />
					<view class="u-demo-block__content">
						<u-code ref="uCode2" @change="codeChange2" keep-running start-text="获取验证码"></u-code>
						<text @tap="getCode2" :text="tips2" class="u-page__code-text">{{tips2}}</text>
					</view>
				</view>
				<view class="login_phone">
					邀&nbsp;请&nbsp;码:<input class="IdInput" placeholder="请输入邀请码" type="text" v-model="Incode" />
				</view>
				<view class="yixiaoshi">
					一小时发送短信次数不能超过五次
				</view>
				<view class="uni-btn-v">
					<button type="primary" @click="GoPhone">注册</button>
				</view>
			</view>
		</view>
		<view class="img1"></view>
		<view class="img2"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phonePassword: "",
				newPassword: "",
				phone: '',
				password: '',
				tips2: '',
				tips3: '获取邀请码',
				username: '',
				Incode: '',
				type: ''
			}
		},
		onLoad(option) {
			if (option.type && option.sharer) {
				this.type = option.sharer
				this.Incode = option.sharer
			}
		},
		methods: {
			changeCheck() {
				this.checked = !this.checked;
			},
			codeChange2(text) {
				this.tips2 = text;
			},
			GoPhone() {
				this.$http({
					url: '/MemberLogin/appRegister',
					data: {
						memberName: this.phone,
						verificationCode: this.password,
						sharer: this.Incode,
						userName: this.username,
						password: this.phonePassword
					},
					method: 'POST'
				}).then(res => {
					if (res.data.code === 200) {
						uni.showToast({
							title: '注册成功',
							icon: 'success'
						})
						if (this.type) {
							uni.navigateTo({
								url: "/pages/download/download"
							})
						} else {
							uni.switchTab({
								url: '/pages/user/user'
							})
						}

					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
					}
				})
			},
			getCode3() {
				uni.scanCode({
					scanType: ['qrCode'],
					success: (res) => {
						if (res.result) {
							let list = res.result.split("&")[1]
							let arr = list.split("=")
							this.Incode = arr[1]
						}
					}
				})
			},
			getCode2() {
				if (!this.phone) {
					callback1(new Error('手机号不能为空'))
				} else if (this.phonePassword === '') {
					uni.showModal({
						title: '登录密码不能为空',
					});
				} else if (this.phonePassword !== this.newPassword) {
					uni.showModal({
						title: '两次输入密码不一致',
					});
				} else if (this.phone != '') {
					var reg =
						/^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/;

					if (!reg.test(this.phone)) {
						callback(new Error('请输入有效的手机号码'));

					} else {
						if (this.$refs.uCode2.canGetCode) {
							// 模拟向后端请求验证码
							uni.showLoading({
								title: '正在获取验证码'
							})
							this.$http({
								url: "/MemberLogin/sendSms",
								data: {
									templateId: 'SMS_241100166',
									phoneNumberSet: this.phone
								}
							}).then(res => {
								if (res.data.code != 200) {
									uni.hideLoading();
									uni.showToast({
										title: res.data.message
									})
									return
								} else {
									setTimeout(() => {
										uni.hideLoading();
										uni.$u.toast('验证码已发送');
										this.$refs.uCode2.start();
									}, 2000);
								}
							})

						} else {
							uni.$u.toast('请等待时间季结束');
						}
					}

				}

				function callback1(Error) {
					uni.showModal({
						title: '手机号码不能为空',
					});
				}

				function callback(Error) {
					uni.showModal({
						title: '请输入有效的手机号码',
					});

				}
			}
		}
	}
</script>

<style lang="less">
	@import './register.css';
</style>
